package com.yanyeori.framework.log.common.config;

import com.yanyeori.framework.log.common.configprop.LogConfigProps;
import com.yanyeori.framework.log.common.interceptor.LogInterceptor;
import com.yanyeori.framework.log.common.interceptor.MdcInterceptor;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * LogInterceptor配置
 *
 * @author chenkuan 2022/11/18
 */
public class LogInterceptorConfig implements WebMvcConfigurer {

    private final LogConfigProps logConfigProps;

    private static final String[] EXCLUDE_PATH = {
            "/error/**",
            "/swagger-resources/**",
            "/webjars/**",
            "/v2/api-docs",
            "/v3/api-docs",
            "/doc.html",
            "/favicon.ico"
    };

    public LogInterceptorConfig(LogConfigProps logConfigProps) {
        this.logConfigProps = logConfigProps;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MdcInterceptor())
                .excludePathPatterns(EXCLUDE_PATH)
                .order(Ordered.HIGHEST_PRECEDENCE);

        registry.addInterceptor(new LogInterceptor(logConfigProps))
                .excludePathPatterns(EXCLUDE_PATH)
                .order(Ordered.HIGHEST_PRECEDENCE + 1);
    }
}
